@@ -38,6 +38,31 @@ module ActiveRecord::ConnectionAdapters |
||
38 | 38 |
def migration_keys |
39 | 39 |
super + [:charset, :collation] |
40 | 40 |
end |
41 |
+ |
|
42 |
+ def utf8mb4_supported? |
|
43 |
+ if @utf8mb4_supported.nil? |
|
44 |
+ @utf8mb4_supported = !select("show character set like 'utf8mb4'").empty? |
|
45 |
+ else |
|
46 |
+ @utf8mb4_supported |
|
47 |
+ end |
|
48 |
+ end |
|
49 |
+ |
|
50 |
+ def charset_collation(charset, collation) |
|
51 |
+ [charset, collation].map { |name| |
|
52 |
+ case name |
|
53 |
+ when nil |
|
54 |
+ nil |
|
55 |
+ when /\A(utf8mb4(_\w*)?)\z/ |
|
56 |
+ if utf8mb4_supported? |
|
57 |
+ $1 |
|
58 |
+ else |
|
59 |
+ "utf8#{$2}" |
|
60 |
+ end |
|
61 |
+ else |
|
62 |
+ name.to_s |
|
63 |
+ end |
|
64 |
+ } |
|
65 |
+ end |
|
41 | 66 |
end |
42 | 67 |
|
43 | 68 |
prepend CharsetSupport |
@@ -52,12 +77,14 @@ module ActiveRecord::ConnectionAdapters |
||
52 | 77 |
end |
53 | 78 |
|
54 | 79 |
def add_column_options!(sql, options) |
55 |
- if options[:charset] |
|
56 |
- sql << " CHARACTER SET #{options[:charset]}" |
|
80 |
+ charset, collation = @conn.charset_collation(options[:charset], options[:collation]) |
|
81 |
+ |
|
82 |
+ if charset |
|
83 |
+ sql << " CHARACTER SET #{charset}" |
|
57 | 84 |
end |
58 | 85 |
|
59 |
- if options[:collation] |
|
60 |
- sql << " COLLATE #{options[:collation]}" |
|
86 |
+ if collation |
|
87 |
+ sql << " COLLATE #{collation}" |
|
61 | 88 |
end |
62 | 89 |
|
63 | 90 |
super |